/**
 * Created by zhang on 2016/1/20.
 */
ige.service('DepartmentService', ['$mdDialog', '$http', '$cookieStore', function ($mdDialog, $http, $cookieStore) {
    var showErrorDlg = function (title, content) {
        $mdDialog.show(
            $mdDialog.alert()
                .clickOutsideToClose(true)
                .title(title)
                .textContent(content)
                .ok('确定')
        );
    };

    var headers = {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        Authorization: $cookieStore.get('access_token')
    };
    var getListApi = ige.systemSetting.apiDomain + 'web/department/list';
    var deleteApi = ige.systemSetting.apiDomain + 'web/department/delete';
    var addApi = ige.systemSetting.apiDomain + 'web/department/add';
    var editApi = ige.systemSetting.apiDomain + 'web/department/update';

    return {
        getDepartments: function (pageSkip, pageSize, filter, successCallback) {

            var promise = $http.post(getListApi,
                {
                    pageSkip: pageSkip,
                    pageSize: pageSize,
                    filter: filter
                }
                , {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback(response.data.data);
                } else {
                    showErrorDlg('数据获取错误', response.data.message);
                }
            }, function (response) {
                showErrorDlg('数据获取错误', response.statusText);
            });

            return promise;
        },
        deleteDepartment: function (id, successCallback) {
            var promise = $http.post(deleteApi,
                {id: id}
                , {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('删除出错', response.data.message);

                }
            }, function (response) {
                showErrorDlg('删除出错', response.statusText);
            });

            return promise;
        },
        addDepartment: function (depName, desc, successCallback) {
            var promise = $http.post(addApi,
                {name: depName, description: desc}
                , {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('添加出错', response.data.message);
                }
            }, function (response) {
                showErrorDlg('添加出错', response.statusText);
            });

            return promise;
        },
        editDepartment: function (id, depName, desc, successCallback) {
            var promise = $http.post(editApi,
                {id: id, name: depName, description: desc}
                , {
                    headers: headers
                }).then(function (response) {
                if (response.data.is_ok) {
                    successCallback()
                } else {
                    showErrorDlg('更新产生错误', response.data.message);
                }
            }, function (response) {
                showErrorDlg('更新产生错误', response.statusText);
            });

            return promise;
        }
    }
}]);